/**
 * 
 * @authors Riche
 * @date    2016-07-05 10:39:22
 * @version 1.1.0
 */

var dataID = $.getUrlVar("id");
var type = $.getUrlVar("type");

/*将tab数与type数对应*/
type==="0" ? type=1 : type === "1" ? type=3 : type=2;

var vm=new Vue({
  el:"#app",
  data:{
    addTag:'',
    tagOp:'确认',
    tagName:[]

  },
  created:function(){
    var _self = this;
    var data = {
      dataID : dataID
    }

    //data = Common.jsonEncode(data);
    /*Common.ajaxCall({ url: 'user_getCollectionTag', datatype: 'json', type: 'get', data: data}, function (data) {
        if(data.error_code==0){
            _self.tagName = data.data;
        }else{
            alert(data.msg);
        }
    });*/
    Common.ajaxCall({ url: 'user_getMyCollectionTag', datatype: 'json', type: 'post', data: data}, function (data) {
        if(data.error_code==0){
            _self.tagName = data.data;
        }else{
            alert(data.msg);
        }
    });
    /*_self.tagName = {
              unselected: ["文档", "话题", "测试一"],
              selected: ["提问","测试二"]
            };*/
    
    /*$('#title').bind('focus',function(){
      _self.tagOp = "添加";
    });
    $(document).on('click','.cateList li',function(){
      _self.tagOp = "确定";
    });*/
  },
  ready:function(){
    $('#title').focus();
  },
  methods:{
    addTags:function(){
      var _self = this;

      if(_self.tagOp === "添加"){  //添加标签状态
        if(_self.addTag.trim().length===0){
          alert('请输入标签');
          return;
        } 
        var tag = {
          "name":_self.addTag
        }
        
        //新标签调用接口
        var data = {
          dataID : dataID,
          type : type,
          tags : _self.addTag
        }

        //data = Common.jsonEncode(data);
        Common.ajaxCall({ url: 'user_addCollectionTag', datatype: 'json', type: 'get', data: data}, function (data) {
            if(data.error_code==0){
                _self.tagName.push(tag);  //将输入的标签加进tagName里
                _self.addTag = '';
                _self.tagOp = "确定";
            }else{
                alert(data.msg);
            }
        });
      }else{                     //设置标签状态
        var tags = [];
        var cateList = $('.cateList li');
        [].forEach.call(cateList,function(val,index,context){
          if($(val).hasClass('on')){
            tags.push($(val).find('a').html());
          }
        });

        var data = {
          dataID : dataID,
          type : type,
          tags : tags.join(',')
        }

        //data = Common.jsonEncode(data);
        Common.ajaxCall({ url: 'user_setCollectionTag', datatype: 'json', type: 'get', data: data}, function (data) {
            if(data.error_code==0){
                alert(data.msg);
                // setTimeout(function(){   //延迟跳转
                //   window.history.go(-1);
                // },500);
            }else{
                alert(data.msg);
            }
        });
      }
    },
    selTag:function(e){
      if($(e.target).parents('li').hasClass('on')){
        $(e.target).parents('li').removeClass('on');
      }else{
        $(e.target).parents('li').addClass('on');
      }
    }
  },
  watch:{
    "answerInfo,topicInfo": function(){
      this.loadPhoto();
    }
  }
});